import Vue from "./Vue";
import { Dep } from "./Dep";

export class Watcher {
  private readonly vm: Vue;
  private readonly key: string;
  private readonly cb: (params: any) => void;
  private readonly oldValue: any;
  constructor(vm: Vue, key: string, cb: (params: any) => void) {
    this.vm = vm;
    this.key = key;
    this.cb = cb;
    Dep.target = this;
    // @ts-ignore
    this.oldValue = vm.$data[key];
    Dep.target = null;
  }
  update() {
    // @ts-ignore
    const newValue = this.vm[this.key];
    if (newValue === this.oldValue) return;
    this.cb(newValue);
  }
}
